package sedh.project

import grails.test.*

class AlocadorMunProgTests extends GrailsUnitTestCase {
    def alocMunProg
	def p1, m1
    protected void setUp() {
        super.setUp()
		
		 p1 = new Programa(nome: "Programa1", descricao: "Descricao1")
		 m1 = new Municipio (nome: "municipio1", habitantes: 100000)
		
		mockForConstraintsTests(AlocadorMunProg)
		alocMunProg = new AlocadorMunProg(municipio: m1, programa: p1, telefone: 35412213, responsavel: "Responsavel1", familias: 100)
    }

    protected void tearDown() {
        super.tearDown()
    }

	void testAtributos() {
		assertEquals alocMunProg.municipio.nome, "municipio1"
		assertEquals alocMunProg.municipio.habitantes, 100000
		
		assertEquals alocMunProg.programa.nome, "Programa1"
		assertEquals alocMunProg.programa.descricao, "Descricao1"
		
		assertEquals alocMunProg.telefone, "35412213"
		assertEquals alocMunProg.responsavel, "Responsavel1"
		assertEquals alocMunProg.familias, 100
		
	}

	void testBlank() {
        alocMunProg = new AlocadorMunProg(municipio: m1, programa: p1, telefone: "" , responsavel: "", familias: "")
        assertFalse alocMunProg.validate()
        

        alocMunProg = new AlocadorMunProg(municipio: m1, programa: p1, telefone: 35412213, responsavel: "Responsavel1", familias: 100)
        assertTrue alocMunProg.validate()
    }
	
	void testNullable() {
		alocMunProg = new AlocadorMunProg()
		assertFalse alocMunProg.validate()
		assertEquals 'Municipio eh null.', 'nullable', alocMunProg.errors['municipio']
		assertEquals 'Programa eh null', 'nullable', alocMunProg.errors['programa']
		assertEquals 'Telefone eh null', 'nullable', alocMunProg.errors['telefone']
		assertEquals 'Responsavel eh null', 'nullable', alocMunProg.errors['responsavel']
		assertEquals 'Familia eh null', null, alocMunProg.errors['familias']
		
	}
	
	void testMaxSize() {
		alocMunProg.responsavel = 'ResponsavelGrande'
		alocMunProg.telefone = 123456789101112 
		assertFalse alocMunProg.validate()
		assertEquals 'Telefone muito grande.', 'size', alocMunProg.errors['telefone']
		assertEquals 'Responsavel muito grade.', 'size', alocMunProg.errors['responsavel']

		alocMunProg.responsavel = 'Resonsavel'
		alocMunProg.telefone = '123456789'
		assertTrue alocMunProg.validate()
	}
	
	void testMinSize() {
		alocMunProg.familias = -10
		alocMunProg.responsavel = "re"
		alocMunProg.telefone = 1234567
		assertFalse alocMunProg.validate()
		assertEquals 'familias eh negativa.', 'min', alocMunProg.errors['familias']
		assertEquals 'responsavel com nome pequeno.', 'size', alocMunProg.errors['responsavel']
		assertEquals 'telefone faltando digitos.', 'size', alocMunProg.errors['telefone']
		
		alocMunProg.responsavel = 'Resonsavel'
		alocMunProg.telefone = '123456789'
		alocMunProg.familias = 100
		assertTrue alocMunProg.validate()
		
	}
}
